<!DOCTYPE html>
<html lang="en">

<!-- Head tag -->
<head><meta name="generator" content="Hexo 3.8.0">

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!--Description-->

    

    
        <meta name="description" content="比较简单的类，主要说一下几个注意的地方：
一、配置AVAudioSession12345678AVAudioSession *session = [AVAudioSession sharedInstance];    NSError *error;    if (![session setCateg">
    

    <!--Author-->
    
        <meta name="author" content="Cyrus">
    

    <!--Open Graph Title-->
    
        <meta property="og:title" content="AVFoundation学习笔记五 AVAudioRecorder">
    

    <!--Open Graph Description-->
    
        <meta property="og:description" content="比较简单的类，主要说一下几个注意的地方：
一、配置AVAudioSession12345678AVAudioSession *session = [AVAudioSession sharedInstance];    NSError *error;    if (![session setCateg">
    

    <!--Open Graph Site Name-->
        <meta property="og:site_name" content="Cyrus的技术空间">

    <!--Type page-->
    
        <meta property="og:type" content="article">
    

    <!--Page Cover-->
    
    
        <meta property="og:image" content="http://cyrus_huang.gitee.io/bloghttp://www.codeblocq.com/assets/projects/hexo-theme-clean-blog/img/home-bg.jpg">
    

        <meta name="twitter:card" content="summary_large_image">

    

    
        <meta name="twitter:image" content="http://cyrus_huang.gitee.io/bloghttp://www.codeblocq.com/assets/projects/hexo-theme-clean-blog/img/home-bg.jpg">
    

    <!-- Title -->
    
    <title>AVFoundation学习笔记五 AVAudioRecorder - Cyrus的技术空间</title>

    <!-- Bootstrap Core CSS -->
    <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom CSS -->
    <link rel="stylesheet" href="/blog/css/style.css">

    <!-- Custom Fonts -->
    <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" type="text/css">
    <link href="//fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic" rel="stylesheet" type="text/css">
    <link href="//fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="//oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
    <script src="//oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <!-- Gallery -->
    <link href="//cdnjs.cloudflare.com/ajax/libs/featherlight/1.3.5/featherlight.min.css" type="text/css" rel="stylesheet">

    <!-- Google Analytics -->
    


    <!-- favicon -->
    

</head>


<body>

    <!-- Menu -->
    <!-- Navigation -->
<nav class="navbar navbar-default navbar-custom navbar-fixed-top">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header page-scroll">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="/blog/">Cyrus的技术空间</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav navbar-right">
                
                    <li>
                        <a href="/blog/">
                            
                                Home
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="/blog/archives">
                            
                                Archives
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="/blog/tags">
                            
                                Tags
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="/blog/categories">
                            
                                Categories
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="https://github.com/klugjo/hexo-theme-clean-blog">
                            
                                <i class="fa fa-github fa-stack-2x"></i>
                            
                        </a>
                    </li>
                
            </ul>
        </div>
        <!-- /.navbar-collapse -->
    </div>
    <!-- /.container -->
</nav>

    <!-- Main Content -->
    <!-- Page Header -->
<!-- Set your background image for this header in your post front-matter: cover -->

<header class="intro-header" style="background-image: url('http://www.codeblocq.com/assets/projects/hexo-theme-clean-blog/img/home-bg.jpg')">
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <div class="post-heading">
                    <h1>AVFoundation学习笔记五 AVAudioRecorder</h1>
                    
                    <span class="meta">
                        <!-- Date and Author -->
                        
                            Posted by Cyrus on
                        
                        
                            2018-11-02
                        
                    </span>
                </div>
            </div>
        </div>
    </div>
</header>

<!-- Post Content -->
<article>
    <div class="container">
        <div class="row">

            <!-- Tags and categories -->
           
                <div class="col-lg-4 col-lg-offset-2 col-md-5 col-md-offset-1 post-tags">
                    
                        

<a href="/blog/categories/AVFoundation/">AVFoundation</a>

                    
                </div>
                <div class="col-lg-4 col-md-5 post-categories">
                    
                </div>
            

            <!-- Gallery -->
            

            <!-- Post Main Content -->
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <p><img src="http://cyrus_huang.gitee.io/blog/2018/11/02/AVFoundation学习笔记五-AVAudioRecorder/recorder.png" alt=""><br>比较简单的类，主要说一下几个注意的地方：</p>
<h3 id="一、配置AVAudioSession"><a href="#一、配置AVAudioSession" class="headerlink" title="一、配置AVAudioSession"></a>一、配置AVAudioSession</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">AVAudioSession *session = [AVAudioSession sharedInstance];</span><br><span class="line">    NSError *error;</span><br><span class="line">    if (![session setCategory:AVAudioSessionCategoryPlayAndRecord error:&amp;error]) &#123;</span><br><span class="line">        NSLog(@&quot;Category Error: %@&quot;, [error localizedDescription]);</span><br><span class="line">    &#125;</span><br><span class="line">    if (![session setActive:YES error:&amp;error]) &#123;</span><br><span class="line">        NSLog(@&quot;Activation Error: %@&quot;, [error localizedDescription]);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h3 id="二、settings参数"><a href="#二、settings参数" class="headerlink" title="二、settings参数"></a>二、settings参数</h3><p>创建AVAudioRecorder的方法中，settings是最重要的参数，有着众多的配置参数：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">- (nullable instancetype)initWithURL:(NSURL *)url settings:(NSDictionary&lt;NSString *, id&gt; *)settings error:(NSError **)outError;</span><br></pre></td></tr></table></figure></p>
<p>settings中的key可以在<strong><em>AVFoundation-&gt;framework文件夹-&gt;AVAudio-&gt;AVAudioSettings.h</em></strong>中找到。</p>
<p>settings常见的key值：</p>
<ul>
<li><strong>AVFormatIDKey</strong>：定义了写入内容的音频格式，值类型存在于 AudioFormatID枚举中，由相应四字节字符组成的32位整形，如：<br><img src="http://cyrus_huang.gitee.io/blog/2018/11/02/AVFoundation学习笔记五-AVAudioRecorder/audio_format.png" alt=""><br><font color="ff0000">注意</font>：指定的类型必须与URL定义的文件名对应，比如录制一个test.wav，隐含的意思是录制的音频必须满足Waveform Audio File Format(WAVE)的格式要求，即低字节序（AVLinearPCMIsBigEndianKey 值为NO）、LinerPCM。如果AudioFormatID的值不是 kAudioFormatLinearPCM。NSError的错误信息为：<br>The  operation couldn’t be completed.(OSStatus error 118449215).<br>118449215 = ‘fmt?’,即不兼容格式</li>
<li><strong>AVSampleRateKey</strong>:采样率，对输入的模拟音频信号每一秒内的采样数，如8kHz,AM广播的录制效果，不件较小。44.1kHz，CD质量的采样率，文件比较大。尽量使用标准采样率，如8000、16000、22050和44100。</li>
<li><strong>AVNumberOfChannelsKey</strong>:通道数，1：单声道  2：立体声</li>
<li><strong>AVLinearPCMBitDepthKey</strong>:采样精度/位深， 8位或16位，用于lpcm</li>
<li><strong>AVLinearPCMIsBigEndianKey</strong>:是否大端保存数据，用于lpcm</li>
<li><strong>AVLinearPCMIsFloatKey</strong>:采样数据是否为浮点型</li>
<li><strong>AVEncoderBitDepthHintKey</strong>:编码位深，8-32，非lpcm使用</li>
<li><strong>AVEncoderAudioQualityKey</strong>:编码质量，非lpcm使用<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">typedef NS_ENUM(NSInteger, AVAudioQuality) &#123;</span><br><span class="line"> 	AVAudioQualityMin    = 0,</span><br><span class="line"> 	AVAudioQualityLow    = 0x20,</span><br><span class="line"> 	AVAudioQualityMedium = 0x40,</span><br><span class="line"> 	AVAudioQualityHigh   = 0x60,</span><br><span class="line"> 	AVAudioQualityMax    = 0x7F</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></li>
</ul>


                
            </div>

            <!-- Comments -->
            
                <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                    


                </div>
            
        </div>
    </div>
</article>

    <!-- Footer -->
    <hr>

<!-- Footer -->
<footer>
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <ul class="list-inline text-center">
                    

                    

                    
                        <li>
                            <a href="https://github.com/klugjo/hexo-theme-clean-blog" target="_blank">
                                <span class="fa-stack fa-lg">
                                    <i class="fa fa-circle fa-stack-2x"></i>
                                    <i class="fa fa-github fa-stack-1x fa-inverse"></i>
                                </span>
                            </a>
                        </li>
                    

                    

                    

                    
                </ul>
                <p class="copyright text-muted">&copy; 2020 Cyrus<br></p>
                <p class="copyright text-muted">Original Theme <a target="_blank" href="http://startbootstrap.com/template-overviews/clean-blog/">Clean Blog</a> from <a href="http://startbootstrap.com/" target="_blank">Start Bootstrap</a></p>
                <p class="copyright text-muted">Adapted for <a target="_blank" href="https://hexo.io/">Hexo</a> by <a href="http://www.codeblocq.com/" target="_blank">Jonathan Klughertz</a></p>
                <p class="copyright text-muted"><a href="http://www.beian.miit.gov.cn">粤ICP备18110122号-1</a></p>
            </div>
        </div>
    </div>
</footer>


    <!-- After footer scripts -->
    
<!-- jQuery -->
<script src="//code.jquery.com/jquery-2.1.4.min.js"></script>

<!-- Bootstrap -->
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<!-- Gallery -->
<script src="//cdnjs.cloudflare.com/ajax/libs/featherlight/1.3.5/featherlight.min.js" type="text/javascript" charset="utf-8"></script>

<!-- Disqus Comments -->



</body>

</html>